load(
    "//tools/skylark:drake_cc.bzl",
    "drake_cc_binary",
    "drake_cc_googletest",
    "drake_cc_library",
    "drake_cc_package_library",
)
load(
    "//tools/skylark:drake_py.bzl",
    "drake_py_binary",
)
load("//tools/install:install.bzl", "install", "install_files")
load("//tools/lint:lint.bzl", "add_lint_tests")

package(default_visibility = ["//visibility:public"])

drake_cc_package_library(
    name = "geometry",
    visibility = ["//visibility:public"],
    deps = [
        ":collision_filter_declaration",
        ":collision_filter_manager",
        ":drake_visualizer",
        ":drake_visualizer_params",
        ":geometry_frame",
        ":geometry_ids",
        ":geometry_instance",
        ":geometry_properties",
        ":geometry_roles",
        ":geometry_set",
        ":geometry_state",
        ":geometry_version",
        ":internal_frame",
        ":internal_geometry",
        ":kinematics_vector",
        ":make_mesh_for_deformable",
        ":meshcat",
        ":meshcat_animation",
        ":meshcat_graphviz",
        ":meshcat_point_cloud_visualizer",
        ":meshcat_visualizer",
        ":meshcat_visualizer_params",
        ":proximity_engine",
        ":proximity_properties",
        ":read_obj",
        ":rgba",
        ":scene_graph",
        ":scene_graph_inspector",
        ":shape_specification",
        ":shape_to_string",
        ":utilities",
    ],
)

drake_cc_library(
    name = "proximity_engine",
    srcs = [
        "proximity_engine.cc",
    ],
    hdrs = [
        "proximity_engine.h",
    ],
    interface_deps = [
        "//common:default_scalars",
        "//common:sorted_pair",
        "//geometry/proximity:collision_filter",
        "//geometry/proximity:deformable_contact_internal",
        "//geometry/proximity:hydroelastic_internal",
        "//geometry/proximity:make_mesh_from_vtk",
        "//geometry/query_results",
        "//math",
        ":geometry_ids",
        ":geometry_roles",
        ":shape_specification",
        ":internal_geometry",
    ],
    deps = [
        ":read_obj",
        ":utilities",
        "//geometry/proximity",
        "//geometry/proximity:collisions_exist_callback",
        "//geometry/proximity:deformable_contact_geometries",
        "//geometry/proximity:distance_to_point_callback",
        "//geometry/proximity:distance_to_shape_callback",
        "//geometry/proximity:find_collision_candidates_callback",
        "//geometry/proximity:hydroelastic_callback",
        "//geometry/proximity:obj_to_surface_mesh",
        "//geometry/proximity:penetration_as_point_pair_callback",
        "@fcl_internal//:fcl",
        "@fmt",
    ],
)

drake_cc_library(
    name = "collision_filter_declaration",
    srcs = ["collision_filter_declaration.cc"],
    hdrs = ["collision_filter_declaration.h"],
    deps = [
        ":geometry_set",
        "//common:essential",
    ],
)

drake_cc_library(
    name = "collision_filter_manager",
    srcs = ["collision_filter_manager.cc"],
    hdrs = ["collision_filter_manager.h"],
    deps = [
        ":collision_filter_declaration",
        ":geometry_ids",
        ":geometry_set",
        "//geometry/proximity:collision_filter",
    ],
)

drake_cc_library(
    name = "drake_visualizer",
    srcs = ["drake_visualizer.cc"],
    hdrs = ["drake_visualizer.h"],
    interface_deps = [
        ":drake_visualizer_params",
        ":geometry_roles",
        ":geometry_version",
        ":scene_graph",
        "//common:essential",
        "//lcm:interface",
        "//systems/framework:context",
        "//systems/framework:leaf_system",
    ],
    deps = [
        "//lcm:drake_lcm",
        "//lcmtypes:viewer",
        "//systems/lcm:lcm_system_graphviz",
    ],
)

drake_cc_library(
    name = "drake_visualizer_params",
    hdrs = ["drake_visualizer_params.h"],
    deps = [
        ":geometry_roles",
        ":rgba",
        "//common:name_value",
    ],
)

drake_cc_library(
    name = "kinematics_vector",
    srcs = [
        "kinematics_vector.cc",
    ],
    hdrs = [
        "kinematics_vector.h",
    ],
    interface_deps = [
        ":geometry_ids",
        "//common:essential",
        "//math:geometric_transform",
    ],
    deps = [
        "//common:default_scalars",
        "//common:nice_type_name",
    ],
)

drake_cc_library(
    name = "geometry_frame",
    srcs = ["geometry_frame.cc"],
    hdrs = ["geometry_frame.h"],
    deps = [
        ":geometry_ids",
        "//common:essential",
    ],
)

drake_cc_library(
    name = "geometry_ids",
    srcs = [],
    hdrs = ["geometry_ids.h"],
    deps = ["//common:identifier"],
)

drake_cc_library(
    name = "geometry_instance",
    srcs = ["geometry_instance.cc"],
    hdrs = ["geometry_instance.h"],
    deps = [
        ":geometry_ids",
        ":geometry_roles",
        ":shape_specification",
        ":utilities",
        "//common:copyable_unique_ptr",
        "//common:essential",
        "//geometry/proximity:volume_mesh",
    ],
)

drake_cc_library(
    name = "geometry_properties",
    srcs = ["geometry_properties.cc"],
    hdrs = ["geometry_properties.h"],
    deps = [
        ":rgba",
        "//common:essential",
        "//common:value",
        "@fmt",
    ],
)

drake_cc_library(
    name = "geometry_version",
    srcs = ["geometry_version.cc"],
    hdrs = ["geometry_version.h"],
    deps = [
        ":geometry_roles",
        "//common:essential",
        "//common:identifier",
    ],
)

drake_cc_library(
    name = "geometry_roles",
    srcs = ["geometry_roles.cc"],
    hdrs = ["geometry_roles.h"],
    deps = [
        ":geometry_properties",
        "//common:essential",
    ],
)

drake_cc_library(
    name = "geometry_set",
    hdrs = ["geometry_set.h"],
    deps = [":geometry_ids"],
)

drake_cc_library(
    name = "geometry_state",
    srcs = ["geometry_state.cc"],
    hdrs = ["geometry_state.h"],
    deps = [
        ":collision_filter_manager",
        ":geometry_frame",
        ":geometry_ids",
        ":geometry_instance",
        ":geometry_set",
        ":geometry_version",
        ":internal_frame",
        ":internal_geometry",
        ":kinematics_vector",
        ":proximity_engine",
        ":utilities",
        "//geometry/render:render_engine",
        "//math:gradient",
    ],
)

drake_cc_library(
    name = "make_mesh_for_deformable",
    srcs = ["make_mesh_for_deformable.cc"],
    hdrs = ["make_mesh_for_deformable.h"],
    deps = [
        ":shape_specification",
        "//geometry/proximity:make_mesh_from_vtk",
        "//geometry/proximity:make_sphere_mesh",
        "//geometry/proximity:volume_mesh",
    ],
)

drake_cc_library(
    name = "proximity_properties",
    srcs = ["proximity_properties.cc"],
    hdrs = ["proximity_properties.h"],
    deps = [
        ":geometry_roles",
        "//multibody/plant:coulomb_friction",
    ],
)

drake_cc_library(
    name = "scene_graph",
    srcs = [
        "query_object.cc",
        "scene_graph.cc",
    ],
    hdrs = [
        "query_object.h",
        "scene_graph.h",
    ],
    deps = [
        ":geometry_state",
        ":scene_graph_inspector",
        "//common:essential",
        "//common:nice_type_name",
        "//geometry/query_results:contact_surface",
        "//geometry/query_results:penetration_as_point_pair",
        "//geometry/query_results:signed_distance_pair",
        "//geometry/query_results:signed_distance_to_point",
        "//systems/framework",
    ],
)

drake_cc_library(
    name = "scene_graph_inspector",
    srcs = ["scene_graph_inspector.cc"],
    hdrs = ["scene_graph_inspector.h"],
    deps = [":geometry_state"],
)

drake_cc_library(
    name = "internal_frame",
    srcs = ["internal_frame.cc"],
    hdrs = ["internal_frame.h"],
    deps = [
        ":geometry_ids",
        "//common:essential",
    ],
)

drake_cc_library(
    name = "internal_geometry",
    srcs = ["internal_geometry.cc"],
    hdrs = ["internal_geometry.h"],
    deps = [
        ":geometry_ids",
        ":geometry_roles",
        ":internal_frame",
        ":make_mesh_for_deformable",
        ":shape_specification",
        "//common:copyable_unique_ptr",
        "//common:essential",
        "//geometry/proximity:volume_mesh",
        "//math:geometric_transform",
    ],
)

drake_cc_library(
    name = "read_obj",
    srcs = ["read_obj.cc"],
    hdrs = ["read_obj.h"],
    interface_deps = [
        "@eigen",
    ],
    deps = [
        "//common:essential",
        "@fmt",
        "@tinyobjloader_internal//:tinyobjloader",
    ],
)

drake_cc_library(
    name = "shape_specification",
    srcs = ["shape_specification.cc"],
    hdrs = ["shape_specification.h"],
    deps = [
        "//common:essential",
        "//common:nice_type_name",
        "//geometry/proximity:meshing_utilities",
        "//geometry/proximity:obj_to_surface_mesh",
        "//math:geometric_transform",
    ],
)

drake_cc_library(
    name = "shape_to_string",
    srcs = ["shape_to_string.cc"],
    hdrs = ["shape_to_string.h"],
    deps = [
        ":shape_specification",
        "@fmt",
    ],
)

drake_cc_library(
    name = "utilities",
    srcs = ["utilities.cc"],
    hdrs = ["utilities.h"],
    deps = [
        "//common",
        "//math:geometric_transform",
    ],
)

drake_cc_library(
    name = "rgba",
    srcs = ["rgba.cc"],
    hdrs = ["rgba.h"],
    deps = [
        "//common",
        "@fmt",
    ],
)

genrule(
    name = "stats_js_genrule",
    srcs = ["@statsjs//:build/stats.min.js"],
    outs = ["stats.min.js"],
    cmd = "cp $< $@",
    visibility = ["//visibility:private"],
)

genrule(
    name = "meshcat_ico_genrule",
    srcs = ["//doc:favicon.ico"],
    outs = ["meshcat.ico"],
    cmd = "cp $< $@",
    visibility = ["//visibility:private"],
)

genrule(
    name = "meshcat_js_genrule",
    srcs = ["@meshcat//:dist/main.min.js"],
    outs = ["meshcat.js"],
    cmd = "cp $< $@",
    visibility = ["//visibility:private"],
)

filegroup(
    name = "meshcat_resources",
    srcs = [
        "meshcat.html",
        ":meshcat.ico",
        ":meshcat.js",
        ":stats.min.js",
    ],
    visibility = ["//visibility:private"],
)

install_files(
    name = "install_meshcat_resources",
    dest = "share/drake/geometry",
    files = [":meshcat_resources"],
)

drake_cc_library(
    name = "meshcat_animation",
    srcs = ["meshcat_animation.cc"],
    hdrs = ["meshcat_animation.h"],
    deps = [
        "//math:geometric_transform",
    ],
)

drake_cc_googletest(
    name = "meshcat_animation_test",
    deps = [
        ":meshcat_animation",
        "//common/test_utilities:eigen_matrix_compare",
        "//common/test_utilities:expect_throws_message",
    ],
)

drake_cc_library(
    name = "meshcat",
    srcs = ["meshcat.cc"],
    hdrs = [
        "meshcat.h",
        "meshcat_types.h",
    ],
    data = [":meshcat_resources"],
    install_hdrs_exclude = ["meshcat_types.h"],
    interface_deps = [
        ":meshcat_animation",
        ":rgba",
        ":shape_specification",
        "//common:essential",
        "//common:name_value",
        "//geometry/proximity:triangle_surface_mesh",
        "//math:geometric_transform",
        "//perception:point_cloud",
    ],
    deps = [
        "//common:drake_export",
        "//common:find_resource",
        "//common:network_policy",
        "//common:scope_exit",
        "@common_robotics_utilities",
        "@msgpack_internal//:msgpack",
        "@stduuid_internal//:stduuid",
        "@uwebsockets_internal//:uwebsockets",
    ],
)

drake_cc_binary(
    name = "meshcat_manual_test",
    testonly = True,
    srcs = ["test/meshcat_manual_test.cc"],
    data = [
        ":environment_maps",
        "//examples/kuka_iiwa_arm:models",
        "//geometry/render:test_models",
        "//manipulation/models/iiwa_description:models",
        "//manipulation/models/ycb:models",
        "//multibody/meshcat:models",
    ],
    visibility = ["//visibility:private"],
    deps = [
        ":meshcat",
        ":meshcat_visualizer",
        "//common/test_utilities:maybe_pause_for_user",
        "//multibody/meshcat:contact_visualizer",
        "//multibody/parsing",
        "//multibody/plant",
        "//systems/analysis:simulator",
    ],
)

drake_py_binary(
    name = "meshcat_websocket_client",
    testonly = True,
    srcs = ["test/meshcat_websocket_client.py"],
    visibility = [
        "//bindings/pydrake/visualization:__pkg__",
    ],
)

drake_cc_googletest(
    name = "meshcat_test",
    data = [
        ":environment_maps",
        ":meshcat_websocket_client",
        "//geometry/render:test_models",
    ],
    tags = [
        "cpu:2",
    ],
    deps = [
        ":meshcat",
        "//common/test_utilities:eigen_matrix_compare",
        "//common/test_utilities:expect_throws_message",
        "@msgpack_internal//:msgpack",
    ],
)

drake_cc_googletest(
    name = "meshcat_denied_test",
    allow_network = ["none"],
    deps = [
        ":meshcat",
        "//common/test_utilities:expect_throws_message",
    ],
)

drake_cc_library(
    name = "meshcat_point_cloud_visualizer",
    srcs = ["meshcat_point_cloud_visualizer.cc"],
    hdrs = ["meshcat_point_cloud_visualizer.h"],
    deps = [
        ":meshcat",
        ":meshcat_graphviz",
        ":rgba",
        ":utilities",
        "//perception:point_cloud",
        "//systems/framework:leaf_system",
    ],
)

drake_cc_googletest(
    name = "meshcat_point_cloud_visualizer_test",
    deps = [
        ":meshcat_point_cloud_visualizer",
        "//common/test_utilities:expect_throws_message",
        "//systems/analysis:simulator",
        "//systems/framework:diagram_builder",
        "//systems/primitives:constant_value_source",
    ],
)

drake_cc_library(
    name = "meshcat_graphviz",
    srcs = ["meshcat_graphviz.cc"],
    hdrs = ["meshcat_graphviz.h"],
    deps = [
        "//systems/framework:system_base",
    ],
)

drake_cc_googletest(
    name = "meshcat_graphviz_test",
    deps = [
        ":meshcat_graphviz",
    ],
)

drake_cc_library(
    name = "meshcat_visualizer_params",
    hdrs = ["meshcat_visualizer_params.h"],
    deps = [
        ":geometry_roles",
        ":rgba",
        "//common:name_value",
    ],
)

drake_cc_library(
    name = "meshcat_visualizer",
    srcs = ["meshcat_visualizer.cc"],
    hdrs = ["meshcat_visualizer.h"],
    deps = [
        ":geometry_roles",
        ":meshcat",
        ":meshcat_graphviz",
        ":meshcat_visualizer_params",
        ":rgba",
        ":scene_graph",
        "//common:essential",
        "//math:geometric_transform",
        "//systems/analysis:instantaneous_realtime_rate_calculator",
        "//systems/framework:context",
        "//systems/framework:diagram_builder",
        "//systems/framework:leaf_system",
    ],
)

drake_cc_googletest(
    name = "meshcat_visualizer_test",
    data = [
        "//geometry/render:test_models",
        "//manipulation/models/iiwa_description:models",
        "//multibody/meshcat:models",
    ],
    deps = [
        ":meshcat_visualizer",
        "//common/test_utilities:expect_throws_message",
        "//multibody/parsing",
        "//multibody/plant",
        "//systems/analysis:simulator",
        "//systems/primitives:constant_vector_source",
        "@msgpack_internal//:msgpack",
    ],
)

# -----------------------------------------------------

filegroup(
    name = "test_obj_files",
    testonly = 1,
    srcs = [
        "test/convex.obj",
        "test/extruded_u.obj",
        "test/forbidden_two_cubes.obj",
        "test/non_convex_mesh.obj",
        "test/octahedron.mtl",
        "test/octahedron.obj",
        "test/quad_cube.mtl",
        "test/quad_cube.obj",
    ],
)

filegroup(
    name = "test_vtk_files",
    testonly = 1,
    srcs = [
        "test/cube_as_6_squares.vtk",
        "test/extruded_u_volume_mesh.vtk",
        "test/non_convex_mesh.vtk",
        "test/one_negative_tetrahedron.vtk",
        "test/one_tetrahedron.vtk",
        "test/some_volume.vtk",
        "test/two_tetrahedra_with_field_variable.vtk",
        "test/unstructured.vtk",
    ],
)

filegroup(
    name = "test_stl_files",
    testonly = 1,
    srcs = [
        "test/quad_cube.stl",
    ],
)

filegroup(
    name = "environment_maps",
    testonly = 1,
    srcs = [
        "test/env_256_brick_room.jpg",
        "test/env_256_cornell_box.png",
        "test/env_256_six_color_room.hdr",
        "test/env_256_six_color_room.png",
    ],
)

drake_cc_googletest(
    name = "collision_filter_declaration_test",
    deps = [
        ":collision_filter_declaration",
        ":geometry_ids",
    ],
)

drake_cc_googletest(
    name = "collision_filter_manager_test",
    deps = [
        ":collision_filter_manager",
        ":scene_graph",
        "//common/test_utilities:expect_no_throw",
    ],
)

drake_cc_googletest(
    name = "drake_visualizer_test",
    deps = [
        ":drake_visualizer",
        "//common/test_utilities:eigen_matrix_compare",
        "//common/test_utilities:expect_throws_message",
        "//lcm",
        "//lcmtypes:viewer",
        "//systems/analysis:simulator",
    ],
)

drake_cc_googletest(
    name = "proximity_engine_test",
    data = [
        ":test_obj_files",
        ":test_vtk_files",
    ],
    deps = [
        ":proximity_engine",
        ":shape_specification",
        "//common/test_utilities:eigen_matrix_compare",
        "//common/test_utilities:expect_no_throw",
        "//common/test_utilities:expect_throws_message",
        "//geometry/proximity:hydroelastic_callback",
        "//geometry/proximity:make_sphere_mesh",
        "//math",
        "@fcl_internal//:fcl",
    ],
)

drake_cc_googletest(
    name = "kinematics_vector_test",
    deps = [
        ":kinematics_vector",
        "//common/test_utilities",
        "//common/test_utilities:limit_malloc",
        "//math:geometric_transform",
    ],
)

drake_cc_googletest(
    name = "geometry_frame_test",
    deps = [
        ":geometry_frame",
        "//common:essential",
        "//common/test_utilities:expect_throws_message",
    ],
)

drake_cc_googletest(
    name = "geometry_instance_test",
    deps = [
        ":geometry_instance",
        "//common/test_utilities",
    ],
)

drake_cc_googletest(
    name = "geometry_properties_test",
    deps = [
        ":geometry_properties",
        "//common/test_utilities",
        "//common/test_utilities:limit_malloc",
    ],
)

drake_cc_googletest(
    name = "geometry_version_test",
    deps = [
        ":geometry_version",
    ],
)

drake_cc_googletest(
    name = "geometry_set_test",
    deps = [
        ":geometry_ids",
        ":geometry_set",
        "//common/test_utilities:expect_no_throw",
        "//geometry/test_utilities:geometry_set_tester",
    ],
)

drake_cc_googletest(
    name = "geometry_state_test",
    deps = [
        ":geometry_state",
        "//common/test_utilities",
        "//geometry/proximity:make_sphere_mesh",
        "//geometry/test_utilities:dummy_render_engine",
    ],
)

drake_cc_googletest(
    name = "make_mesh_for_deformable_test",
    data = [
        ":test_vtk_files",
    ],
    deps = [
        ":make_mesh_for_deformable",
        "//common:find_resource",
        "//common/test_utilities:expect_throws_message",
    ],
)

drake_cc_googletest(
    name = "proximity_properties_test",
    deps = [
        ":geometry_roles",
        ":proximity_properties",
        "//common/test_utilities:expect_throws_message",
    ],
)

drake_cc_googletest(
    name = "internal_geometry_test",
    deps = [
        ":internal_geometry",
        "//common/test_utilities:eigen_matrix_compare",
        "//common/test_utilities:expect_no_throw",
        "//common/test_utilities:expect_throws_message",
        "//geometry/proximity:make_sphere_mesh",
    ],
)

drake_cc_googletest(
    name = "scene_graph_test",
    deps = [
        ":scene_graph",
        "//common/test_utilities:eigen_matrix_compare",
        "//common/test_utilities:expect_no_throw",
        "//common/test_utilities:expect_throws_message",
        "//geometry/test_utilities:dummy_render_engine",
    ],
)

drake_cc_googletest(
    name = "query_object_test",
    deps = [
        ":geometry_frame",
        ":geometry_instance",
        ":scene_graph",
        "//common/test_utilities",
    ],
)

drake_cc_googletest(
    name = "scene_graph_inspector_test",
    deps = [
        ":geometry_frame",
        ":scene_graph_inspector",
    ],
)

drake_cc_googletest(
    name = "shape_specification_test",
    data = [
        ":test_obj_files",
        ":test_stl_files",
        ":test_vtk_files",
    ],
    deps = [
        ":shape_specification",
        "//common:find_resource",
        "//common/test_utilities",
    ],
)

drake_cc_googletest(
    name = "shape_to_string_test",
    deps = [
        ":shape_to_string",
    ],
)

drake_cc_googletest(
    name = "utilities_test",
    deps = [
        "utilities",
        "//common/test_utilities",
    ],
)

drake_cc_googletest(
    name = "rgba_test",
    deps = [
        ":rgba",
        "//common/test_utilities",
        "//common/yaml:yaml_io",
    ],
)

drake_cc_googletest(
    name = "read_obj_test",
    data = [":test_obj_files"],
    deps = [
        ":read_obj",
        "//common:find_resource",
        "//common/test_utilities",
    ],
)

install(
    name = "install",
    visibility = ["//visibility:public"],
    deps = [
        ":install_meshcat_resources",
    ],
)

add_lint_tests(enable_clang_format_lint = False)
